# read in politico data and create a clean 2020 dataset

# libs ----
library(tidyverse)
library(here)
library(readxl)

# read in ----
presR <- read_excel(path = '../data/politico_2020/election_2020_state_county.xlsx', sheet = 'prest')
presD <- read_excel(path = '../data/politico_2020/election_2020_state_county.xlsx', sheet = 'presb')
senR <- read_excel(path = '../data/politico_2020/election_2020_state_county.xlsx', sheet = 'senr')
senD <- read_excel(path = '../data/politico_2020/election_2020_state_county.xlsx', sheet = 'send')
govR <- read_excel(path = '../data/politico_2020/election_2020_state_county.xlsx', sheet = 'govr')
govD <- read_excel(path = '../data/politico_2020/election_2020_state_county.xlsx', sheet = 'govd')


pres <- bind_rows(presR, presD) 
pres <- pres %>% mutate(race = 'president', year = 2020)
names(pres) <- c('COUNTY', 'R VOTES', 'R PCT', 'D VOTES', 'D PCT', 'STATE', 'race', 'year')

names(senR) <- c('COUNTY', 'R VOTES', 'R PCT', 'D VOTES', 'D PCT', 'STATE')
names(senD) <- c('COUNTY',  'D VOTES', 'D PCT', 'R VOTES', 'R PCT', 'STATE')
sen <- bind_rows(senR, senD)
sen <- sen %>% mutate(race = 'senate', year = 2020)

gov <- bind_rows(govR, govD)
gov <- gov %>% mutate(race = 'governor', year = 2020)

dat2020 <- bind_rows(pres, sen, gov)

rm("gov", "govD", "govR", "pres", "presD", "presR", "sen", "senD", "senR")

saveRDS(dat2020, '../data/politico_2020/elec2020.Rds')
